<template>
  <div class="divWarehouseAdd">
    <div class="divForm">
      <el-form ref="formAdd" :rules="rules" :model="form">
        <el-row :gutter="30">
          <el-col :span="6">
            <el-form-item label="库区编号" prop="code">
              <el-input v-model="form.code" disabled placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="所属仓库" prop="warehouseId">
              <el-select v-model="form.warehouseId" placeholder="请选择">
                <el-option v-for="item in locationList" :key="item.id" :label="item.name" :value="item.id"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="库区名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="温度类型" prop="temperatureType">
              <el-select v-model="form.temperatureType" placeholder="请选择">
                <el-option v-for="item in temperatureTypeOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="6">
            <el-form-item label="承重类型" prop="bearingType">
              <el-select v-model="form.bearingType" placeholder="请选择">
                <el-option v-for="item in bearingTypeOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="用途属性" prop="useType">
              <el-select v-model="form.useType" placeholder="请选择">
                <el-option v-for="item in useTypeOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="负责人" prop="personName">
              <el-input v-model="form.personName" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="6">
            <div class="radio">
              <el-form-item label="库区状态" prop="status">
                <template>
                  <el-radio v-model="form.status" :label="1">启用</el-radio>
                  <el-radio v-model="form.status" :label="0">停用</el-radio>
                </template>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <div class="buttonBox">
          <el-button round @click="$router.back()">返回</el-button>
          <el-button round @click="add">提交</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getArea, getNext, putArea, addArea, getWarehouseList } from '@/api/ManageBaseInfo'
export default {
  data () {
    return {
      form: {
        bearingType: '',
        code: '',
        name: '',
        personName: '',
        phone: '',
        status: 1,
        temperatureType: '',
        useType: '',
        warehouseId: ''
      },
      rules: {
        name: [{ required: true, message: '请输入库区名称', trigger: 'change' }],
        status: [{ required: true, message: '请选择仓库状态', trigger: 'change' }],
        personName: [{ required: true, message: '请输入负责人', trigger: 'change' }],
        bearingType: [{ required: true, message: '请选择承重类型', trigger: 'change' }],
        code: [{ required: true, message: '请输入库区编号', trigger: 'change' }],
        temperatureType: [{ required: true, message: '请选择温度类型', trigger: 'change' }],
        useType: [{ required: true, message: '请选择用途属性', trigger: 'change' }],
        warehouseId: [{ required: true, message: '请选择所属仓库', trigger: 'change' }]
      },
      locationList: [],
      temperatureTypeOptions: [{
        value: 'CW',
        label: '常温'
      }, {
        value: 'LC',
        label: '冷藏'
      }, {
        value: 'HW',
        label: '恒温'
      }],
      bearingTypeOptions: [{
        value: 'ZX',
        label: '重型'
      }, {
        value: 'LC',
        label: '轻型'
      }, {
        value: 'QX',
        label: '中型'
      }],
      useTypeOptions: [{
        value: 'RKHCQ',
        label: '入库缓存区'
      }, {
        value: 'CKHCQ',
        label: '出库缓存区'
      }, {
        value: 'CCQ',
        label: '存储区'
      }, {
        value: 'FJQ',
        label: '分拣区'
      }, {
        value: 'ZJQ',
        label: '质检区'
      }]
    }
  },
  methods: {
    async setForm () {
      const res = await getWarehouseList({ status: 1 })
      this.locationList = res.data
      if (this.form.id === 'null') {
        const { data } = await getNext('KQ')
        this.form.code = data
      } else {
        const { data } = await getArea(this.form.id)
        this.form = data
      }
    },
    async add () {
      await this.$refs.formAdd.validate(async (valid) => {
        if (valid) {
          if (this.form.id === 'null') {
            await addArea(this.form)
            this.$message.success('新增成功！')
            this.$router.back()
          } else {
            await putArea(this.form)
            this.$message.success('修改成功！')
            this.$router.back()
          }
        } else {
          console.log('表单验证失败')
          return false
        }
      })
    }
  },
  created () {
    this.form.id = this.$route.params.id
    this.setForm()
  }
}
</script>

<style lang="less" scoped>
.divWarehouseAdd {
  box-sizing: border-box;
  height: 480px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 0 6px 0 rgb(144 142 142 / 17%);
  padding-top: 31px;
  padding-bottom: 30px;
  .divForm {
    box-sizing: border-box;
    padding-left: 30px;
    padding-right: 30px;
    .el-col {
      margin-bottom: 30px;
    }
    /deep/ .el-form-item__label {
      margin: 0;
      line-height: 18px;
      padding-bottom: 10px;
      padding-left: 5px;
      font-size: 12px;
      font-family: PingFangSC, PingFangSC-Medium;
      font-weight: 500;
      color: #887e7e;
      text-align: right;
      vertical-align: middle;
      float: left;
    }
    /deep/ input {
      height: 40px;
      line-height: 40px;
      border: 1px solid #f8f5f5;
      background: #f8f5f5;
      border-radius: 6px;
      color: #b5abab;
    }
    /deep/ .is-disabled input {
      background: #f3ebeb;
      border-color: #f3ebeb;
      color: #b5abab;
      border-radius: 6px;
      height: 40px;
      line-height: 40px;
      border: 1px solid #f8f5f5;
    }
    .el-select {
      width: 100%;
    }
    .el-cascader {
      width: 100%;
    }
    .radio {
      display: flex;
      width: 100%;
      .el-radio {
        margin-top: 10px;
      }
    }
    /deep/ .el-input-group__append {
      width: 40px;
      height: 40px;
      background: #e7dfdf;
      border-radius: 0 6px 6px 0;
      text-align: center;
      padding: 0 3px;
      border: 0;
    }
    /deep/ .el-input__inner:hover {
      border: 1px RGB(136, 126, 126) solid;
    }
    /deep/ .el-input__inner:focus {
      border: 1px RGB(255, 178, 0) solid;
    }
  }
  .buttonBox {
    padding: 25px 0 0;
    margin: 0 -30px -5px;
    border-top: 1px solid #f5efee;
    text-align: center;
    button {
      border: none;
      width: 170px;
    }
    button:nth-child(1) {
      background: #f8f5f5;
      color: #332929;
    }
    button:nth-child(2) {
      margin-left: 30px;
      background: #ffb200;
      color: #332929;
    }
  }
}
</style>
